/**
 * Created by forcht on 2018/5/17.
 */
public class Solution {
    public int atoi(String str) {
        int i=0;
        while (i<str.length()&&str.charAt(i)==' ')i++;
        if (i>=str.length())return 0;
        long x=0;
        int p=1;
        if(str.charAt(i)=='-'&&i!=str.length()-1) {
            p = -1;
            i++;
        }else if(str.charAt(i)=='+'&&i!=str.length()-1){
            p=1;
            i++;
        }
        int j=i;
        while (i<str.length()){
            if(str.charAt(i)<'0'||str.charAt(i)>'9') {
                if(j<i)break;
                else return 0;
            }
            x=x*10+str.charAt(i)-'0';
            if(p*x>Integer.MAX_VALUE)return Integer.MAX_VALUE;
            if (p*x<Integer.MIN_VALUE)return Integer.MIN_VALUE;
            i++;
        }
        return (int)x*p;
    }

    public static void main(String[] args) {
        Solution solution=new Solution();
        System.out.println(solution.atoi("-2147483648"));
    }
}
